package pl.perseus.action;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.beans.factory.annotation.Autowired;

import pl.perseus.model.Classes;
import pl.perseus.model.User;
import pl.perseus.service.classes.IClassesService;
import pl.perseus.service.user.IUserService;

public class GetScheduleAction extends BaseAction {

	@Autowired
	IClassesService classesService;
	
	@Autowired
	IUserService userService;
	
	@Getter
	@Setter
	Map<String, Object> result;
	
	@Override
	public String execute() {

		result = new HashMap<String, Object>();
		List<Map<String, String>> items = new LinkedList<Map<String, String>>();
		
		List<Classes> classes = classesService.getClassesForUser(getCurrentUserLogin());

		for(Classes c : classes){
			Map<String, String> scheduleMap = new HashMap<String, String>();
			scheduleMap.put("id", c.getId().toString());
			scheduleMap.put("teacher", getTeachersString(c));
			scheduleMap.put("name", c.getClassname());
			scheduleMap.put("day", c.getSDate());
			scheduleMap.put("time", c.getSHour());
			scheduleMap.put("room", c.getClassroom());
			scheduleMap.put("desc", c.getDescription());
			
			items.add(scheduleMap);
		}
		
		result.put("label", "label");
		result.put("identifier", "id");
		result.put("items", items);
		
		return SUCCESS;
	}
	
	private String getTeachersString(Classes c){
		String s = "";
		for(User u : userService.getTeachersForClasses(c)){
			s += u.getUsername()+"\n";
		}
		return s;
	}

}
